let { HttpRequest, globalData, UserAccess, addUserStat, mfutil,URLChangeCommon } = getApp();
const app = getApp();
Page({
  data: {
    id: null,
    type: null,

    radioActive: false,
    router: '/pages/express_page/express_page',
    pagetitle: '快递到家',
    loadingActive: false
  },
  onLoad(options) {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    let { id, lucky_image, lucky_text, prize_name, prize_sku, cta, type,done_id } = options;
    console.log(options,"3#################");
    this.setData({
      id, lucky_image, lucky_text, prize_name, prize_sku, cta:JSON.parse(cta), type ,done_id
    })
    this.initPage()
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: options.id,
    }
    let track = UserAccess(UserAccessoptions)
    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCover_' + options.id,
      // tileId: id
    }
    console.log(id, '进入抽奖详情页');
    let tracks = addUserStat(customAccessoptions)
  },
  // onShow() {
  //   this.initPage()
  // },
  async initPage() {
    let { id ,prize_sku} = this.data;
    let path = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v2/cms/api/tile/${id}`;
    let parmasObj = {
      member_id_type: 1,
      member_id: globalData.userInfo.memberId
    }
    this.setData({
      loadingActive: true
    })
    let res = await HttpRequest(path, 'POST', parmasObj, {});
  
    console.log(res,"89999999999999999999999999999999999999");
    if (res.data.code == 0) {
      let tilesDetail = res.data.data;
      console.log("2222222222222223", tilesDetail);
        this.data.tilesDetail=tilesDetail;
        this.data.skuid=prize_sku;
      console.log(this.data.skuid,"]]]]]]]]]]]]]]]]]]]]]]]]]]");
      let kpath = `/vans-family/api/v2.1/project/vansFamily/item/getBySkuid?skuid=${this.data.skuid}`
      let kucun = await HttpRequest(kpath, 'GET',{},{});
      console.log(kucun, "kucun---------------------------------------");
      // this.setData({
      //   quantity:this.data.quantity
      // })
      this.data.quantity = kucun.data ? kucun.data.quantity : 1
      console.log(this.data.quantity, "cccccccccccccccccccccccccccc")
      this.setData({
        loadingActive: false
      })
    }
  },
  /**    
   * 快递到家
   */
  expressage() {
    let { id, type, skuid, quantity,done_id,cta,lucky_text,lucky_image } = this.data;
    console.log(this.data.skuid,"cta8888888888888888888");
    if (quantity <= 0) {
      my.alert({
        content: '库存不足，无法参与兑礼!'
      })
      return
    }
    my.redirectTo({
      url: `/pages/storeExpress-form/storeExpress-form?method=expressage&id=${id}&type=${type}&skuid=${skuid}&done_id=${done_id}&cta=${encodeURIComponent(JSON.stringify(cta))}&lucky_text=${lucky_text}&lucky_image=${lucky_image}`
    });
    
    /**
   * 用户行为记录埋点
   */
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let options = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: routerArr[routerArr.length - 1].data.router,
      String4: params ? `params=${params.join(',')}&` : '' + `tileid=${this.data.id}${channel ? '&channel=' + channel : ''}`,
      String3: '快递到家'
    }
    let track = UserAccess(options)
  },

    // btn 链接跳转
  urlChange(event) {


    let { item } = event.currentTarget.dataset;
    console.log(item);

    URLChangeCommon(item)
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: this.data.id,
      String4: item.cta_button_name,

    }
    let track = UserAccess(UserAccessoptions)

    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCTA_' + this.data.tilesDetail.tile_info.id + '_' + item.cta_button_name
      // tileId: this.data.tilesDetail.tile_info.id,
      // ctabtn: this.data.tilesDetail.tile_info.cta[0].cta_button_name
    }

    let tracks = addUserStat(customAccessoptions)
  }
});
